package com.wlstock.funi.request;

import com.android.volley.NetworkResponse;
import com.android.volley.VolleyError;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.zip.GZIPInputStream;

/**
 * Created by admin on 16/8/13.
 */

public class NewStockCalendarRequest implements NetWorkListener, ParseNetResponseListener {


//    private static final String REQ_HOST = "http://datainterface3.eastmoney.com";
//    private static final String REQ_METHOD = "/EM_DataCenter_V3/Api//XGRL/GetXGRL?tkn=eastmoney&secuCode=&startdate=&enddate=&pageSize=50&pageNum=1&sortField=&sortDirec=1&cfg=xgrl";

    private static final String REQ_HOST = "http://m.data.eastmoney.com";
    private static final String REQ_METHOD = "/XG/XgsgList?pagesize=20&page=1";

    private DataResponseListener newStockListListener;

    public NewStockCalendarRequest(DataResponseListener listener){
        this.newStockListListener = listener;
    }

    private NewStockCalendarRequest(){}

    public void request(){
        CustomStringRequest curReq = new CustomStringRequest();
        curReq.networkResponseListener = this;
        HashMap<String, String> header = new HashMap<>();
//        header.put("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
//        header.put("Accept-Encoding","gzip, deflate, sdch");
//        header.put("Accept-Language","zh-CN,zh;q=0.8,en;q=0.6");
//        header.put("Cache-Control","max-age=0");
//        header.put("Upgrade-Insecure-Requests","1");
//        header.put("Host","datainterface3.eastmoney.com");
//        header.put("User-Agent","Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.23 Mobile Safari/537.36\n" +
//                "X-Requested-With:XMLHttpRequest");
//        curReq.parseListener = this;
        curReq.requestGet(REQ_HOST, REQ_METHOD,header, null);

    }

    @Override
    public void onErrorResponse(VolleyError error, EventBusConstants eventCode) {
        
    }

    @Override
    public void onErrorResponse(VolleyError error, String eventCode) {
        if (REQ_METHOD.equals(eventCode)){
            if (newStockListListener!=null){
                newStockListListener.newStockList(null);
            }
        }
    }

    @Override
    public void onResponse(String response, EventBusConstants eventCode) {

    }

    @Override
    public void onResponse(String response, String eventCode) {
        if (REQ_METHOD.equals(eventCode)&& response!=null){
            if (newStockListListener!=null){
                String curDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
                ArrayList<Object> resultList = new ArrayList<>();
                try {
                    JSONObject responseObj = new JSONObject(response);

                    JSONArray sgList = responseObj.getJSONObject("result").getJSONArray("Sglist");
                    for (int i = 0; i<sgList.length(); i++){
                        JSONObject sgItem = sgList.getJSONObject(i);
                        String sgDate = sgItem.getString("SgDate");
                        String isToday = sgItem.getString("IsToday");
                        if (sgDate!=null&& sgDate.equals(curDate) && isToday!=null && "today".equals(isToday)){
                            JSONArray xgList = sgItem.getJSONArray("XgList");
                            if (xgList!=null){
                                for (int j = 0; j<xgList.length();j++){
                                    resultList.add(xgList.getJSONObject(j));
                                }
                            }

                        }
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
                newStockListListener.newStockList(resultList);
            }
        }
    }

    @Override
    public String parseResponse(NetworkResponse response) {
        ByteArrayInputStream bIn = new ByteArrayInputStream(response.data);
        ByteArrayOutputStream bOut = new ByteArrayOutputStream();
        try {
            GZIPInputStream gzipInputStream = new GZIPInputStream(bIn);
            byte[] buffer = new byte[256];
            int n ;
            while((n = gzipInputStream.read(buffer))>0){
                bOut.write(buffer, 0, n);
            }
            String s = bOut.toString();
            return s;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    public  interface DataResponseListener{
        public void newStockList(List<Object> stockList);
    }
}
